<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools :show-before="true">
        <template #before>
          共{{ total }}条数据
        </template>
        <template #after>
          <el-button size="small" type="warning" @click="$router.push('/employees/import')">导入</el-button>
          <el-button size="small" type="danger" @click="exportExcel">导出</el-button>
          <el-button size="small" type="primary" @click="isShowDialog = true">新增员工</el-button>
        </template>
      </page-tools>
      <!-- 放置表格和分页 -->
      <el-card>
        <el-table border type="flex" :data="list">
          <el-table-column align="center" prop="index" label="序号" sortable="" width="80px" />
          <el-table-column align="center" prop="username" label="姓名" sortable="" width="80px" />
          <el-table-column align="center" prop="staffPhoto" label="头像" sortable="" width="120px">
            <template v-slot="{row}">
              <img
                v-fixImg
                :src="row.staffPhoto"
                alt=""
                style="border-radius: 50%; width: 100px; height: 100px; padding: 10px"
              >
            </template>
          </el-table-column>
          <el-table-column align="center" prop="workNumber" label="工号" sortable="" />
          <el-table-column
            align="center"
            prop="formOfEmployment"
            label="聘用形式"
            :formatter="formatEmployment"
            sortable=""
          />
          <el-table-column align="center" prop="departmentName" label="部门" sortable="" width="80px" />
          <el-table-column align="center" prop="timeOfEntry" label="入职时间" sortable="">
            <template v-slot="{row}">
              {{ row.timeOfEntry | formatDate }}
            </template>
          </el-table-column>
          <el-table-column align="center" prop="enableState" label="账户状态" sortable="">
            <template v-slot="{row}">
              <el-switch
                :active-value="1"
                :inactive-value="0"
                :value="row.enableState"
                active-color="#13ce66"
                inactive-color="#ff4949"
              />
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" sortable="" fixed="right" width="280px">
            <template v-slot="{row}">
              <el-button type="text" size="small" @click="$router.push(`/employees/detail/${row.id}`)">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small" @click="showRoleDialog(row.id)">角色</el-button>
              <el-button v-if="checkPerm('delete')" type="text" size="small" @click="delEmployee(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            layout="prev, pager, next"
            :total="total"
            :page-size="pageConfig.size"
            :current-page="pageConfig.page"
            @current-change="currentChange"
          />
        </el-row>
      </el-card>
      <!-- 放置表格和分页 -->
      <!-- 添加员工弹框组件 -->
      <add-employee :is-show-dialog="isShowDialog" @closeDialog="closeDialog" />
      <!-- 添加员工弹框组件 -->

      <!-- 修改角色弹框组件 -->
      <assign-role
        ref="assignRole"
        :is-show-role-dialog="isShowRoleDialog"
        @closeRoleDialog="isShowRoleDialog = false"
      />
      <!-- 修改角色弹框组件 -->
    </div>
  </div>
</template>

<script>
import { delEmployeeAPI, getEmployeeListAPI } from '@/api/employees'
import EmployEnum from '@/constant/employees'
import { export_json_to_excel } from '@/vendor/export2Excel'
import { formatDate } from '@/filters'
import AddEmployee from '@/views/employees/components/AddEmployee'
import AssignRole from '@/views/employees/components/AddEmployee/assign-role.vue'

export default {
  components: { AddEmployee, AssignRole },
  data() {
    return {
      pageConfig: {
        page: 1,
        size: 5
      },
      list: [],
      total: 0,
      // 控制新增员工弹窗
      isShowDialog: false,
      // 控制修改员工角色弹窗
      isShowRoleDialog: false
    }
  },
  async created() {
    this.getEmployeeList()
  },

  methods: {
    // 导出员工
    async exportExcel() {
      const { rows } = await getEmployeeListAPI(
        { page: 1, size: this.total }
      )
      const dist = {
        '工号': 'workNumber',
        '用户名': 'username',
        '手机号': 'mobile',
        '部门': 'departmentName',
        '聘用形式': 'formOfEmployment',
        '入职时间': 'timeOfEntry'
      }
      const header = Object.keys(dist)
      // const header = ['工号', '用户名', '手机号', '部门', '聘用形式', '入职时间']

      // 获取一个新的数组
      const data = rows.map(item => {
        const arr = []
        for (const key in dist) {
          // 遍历中文的可以，获取英文enkey 如 遍历到用户名则获取到 username
          const enKey = dist[key]
          // 通过英文enkey在用户中拿出应该被推入到数组的数据value
          const value = item[enKey]
          // 根据不同的条件推入不同的数值即可
          if (enKey === 'formOfEmployment') {
            const obj = EmployEnum.hireType.find(el => el.id === item.formOfEmployment)
            const formOfEmployment = obj ? obj.value : '未知'
            arr.push(formOfEmployment)
          } else if (enKey === 'timeOfEntry') {
            arr.push(formatDate(item.timeOfEntry))
          } else {
            arr.push(value)
          }
        }
        // arr.push(item.workNumber)
        // arr.push(item.username)
        // arr.push(item.mobile)
        // arr.push(item.departmentName)

        // const obj = EmployEnum.hireType.find(el => el.id === item.formOfEmployment)
        // const formOfEmployment = obj ? obj.value : '未知'
        // arr.push(formOfEmployment)

        // arr.push(formatDate(item.timeOfEntry))
        return arr
      })
      export_json_to_excel({
        header,
        data
      })
    },
    // 删除员工
    async delEmployee(id) {
      await this.$confirm('你确定要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await delEmployeeAPI(id)
      this.$message.success('恭喜，删除成功！！')
      this.getEmployeeList()
    },
    // 初始化获取员工数据
    async getEmployeeList() {
      const { total, rows } = await getEmployeeListAPI(this.pageConfig)
      this.list = rows.map((item, index) => {
        return {
          ...item,
          index: (this.pageConfig.page - 1) * this.pageConfig.size + index + 1
        }
      })
      // console.log(rows)
      this.total = total
    },
    // 点击更换页面，获取数据
    currentChange(newPage) {
      this.pageConfig.page = newPage
      this.getEmployeeList()
    },
    // 判断聘用形式
    formatEmployment(row, col, val) {
      const res = EmployEnum.hireType.find(item => item.id === val)
      return res ? res.value : '未知'
    },
    // 弹窗关闭
    closeDialog() {
      this.getEmployeeList()
      this.isShowDialog = false
    },
    // 员工角色弹窗开启
    async showRoleDialog(id) {
      await this.$refs.assignRole.getUserDetail(id)
      this.isShowRoleDialog = true
    }
  }
}
</script>

  <style>

  </style>
